<?php 
class shopAction extends action{
	public function index(){
		I('shop');
		$oCategory = new shop();
		$data = $oCategory->get_list(0, 20);
		$GLOBALS['smarty']->assign('data', $data);
		$GLOBALS['smarty']->display('shop.html');
	}
	
	public function add(){
		$submit = !isset($_POST['submit'])?null:$_POST['submit'];
		$thumb = null;
		if($submit){
			I('shop');			
			$oCategory = new shop();
			$aRg['name'] = $_POST['name'];
			$aRg['keywords'] = $_POST['keywords'];
			$aRg['start_time'] = strtotime($_POST['start_time']);
			$aRg['end_time'] = strtotime($_POST['end_time']);
			$aRg['brief'] = $_POST['brief'];
			$aRg['region_id'] = intval($_POST['region_id']);
			$aRg['type'] = $_POST['type'];
			$aRg['user_id'] = $_POST['user_id'];
			$aRg['sort'] = isset($_POST['sort'])?$_POST['sort']:1;
			$aRg['is_new'] = !empty($_POST['is_new'])?1:0;
			$aRg['is_best'] = !empty($_POST['is_best'])?1:0;
			$aRg['is_payout'] = !empty($_POST['is_payout'])?1:0;
			$aRg['tag'] = $_POST['tag'];
			$aRg['thumb'] = $thumb;
			$aRg['img'] = $_POST['img'];
			$aRg['content'] = $_POST['content'];
			$aRg['sort'] = $_POST['sort'];
			$aRg['multi'] = $_POST['multi'];
			$aRg['send_time'] = $_POST['send_time'];
			$aRg['brief'] = $_POST['brief'];
			$aRg['is_show'] = $_POST['is_show'];
			$aRg['add_time'] = time();
			$res = $oCategory->add($aRg);
			if(!$res){
				$this->makeError('添加失败！');
			}
			else{
				$this->direct('shop');
			}
		}
		else{
			$GLOBALS['smarty']->assign('act', 'add');
			$GLOBALS['smarty']->display('shop.html');
		}
	}
	
	public function update(){
		$submit = !isset($_POST['submit'])?null:$_POST['submit'];
		if($submit){
			I('shop');
			$oCategory = new shop();
			$shopId = !empty($_POST['id'])?$_POST['id']:null;
			if(!$shopId){
				$this->makeError('非法访问！');
			}
			
			$aRg['name'] = $_POST['name'];
			$aRg['keywords'] = $_POST['keywords'];
			$aRg['parent_id'] = $_POST['parent'];
			$aRg['start_time'] = $_POST['start_time'];
			$aRg['end_time'] = $_POST['end_time'];
			$aRg['brief'] = $_POST['brief'];
			$aRg['region_id'] = $_POST['region_id'];
			$aRg['type'] = $_POST['type'];
			$aRg['user_id'] = $_POST['user_id'];
			$aRg['sort'] = isset($_POST['sort'])?$_POST['sort']:1;
			$aRg['is_new'] = !empty($_POST['is_new'])?1:0;
			$aRg['is_best'] = !empty($_POST['is_best'])?1:0;
			$aRg['is_payout'] = !empty($_POST['is_payout'])?1:0;
			$aRg['tag'] = $_POST['tag'];
			$aRg['thumb'] = $_POST['thumb'];
			$aRg['img'] = $_POST['img'];
			$aRg['content'] = $_POST['content'];
			$aRg['sort'] = $_POST['sort'];
			$aRg['multi'] = $_POST['multi'];
			$aRg['send_time'] = $_POST['send_time'];
			$aRg['brief'] = $_POST['brief'];
			$aRg['is_show'] = $_POST['is_show'];
			$res = $oCategory->add($aRg, ' WHERE shop_id = '.$shopId);
			if(!$res){
				$this->makeError('更新失败！');
			}
			else{
				$this->direct('shop');
			}
		}
		else{
			$shopId = isset($_GET['id'])?$_GET['id']:null;
			if(!$shopId){
				$this->makeError('非法访问！');
			}
			I('shop');
			$oCategory = new shop();
			$row = $oCategory->get_info($shopId);
			$row['start_time'] = date('Y-m-d H:i:s', $row['start_time']);
			$row['end_time'] = date('Y-m-d H:i:s', $row['end_time']);
			
			$GLOBALS['smarty']->assign('act', 'update');
			$GLOBALS['smarty']->assign('row', $row);
			$GLOBALS['smarty']->display('shop.html');
		}		
	}
	
	public function delete(){
		$shopId = isset($_GET['id'])?$_GET['id']:null;
		if(!$shopId){
			$this->makeError('非法访问！');
		}
		I('shop');
		$oCategory = new shop();
		$oCategory->del($shopId);
	}
}
?>